/**
 * 
 */
package com.platform.common.utils;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * <pre>
 * 功能：异常的工具类
 * </pre>
 * 
 * @author wei.gao
 */
public class ExcptionUtils extends org.apache.commons.lang3.exception.ExceptionUtils {
	/**
	 * <pre>
	 * 功能：将CheckedException转换为UncheckedException.
	 * </pre>
	 * 
	 * @param e
	 * @return
	 */
	public static RuntimeException unchecked(Throwable e) {
		if (e instanceof RuntimeException) {
			return (RuntimeException) e;
		} else {
			return new RuntimeException(e);
		}
	}
	
	/**
	 * <pre>
	 * 功能：将ErrorStack转化为String
	 * </pre>
	 * 
	 * @param e
	 * @return
	 */
	public static String getStackTraceAsString(Throwable e) {
		StringWriter stringWriter = new StringWriter();
		e.printStackTrace(new PrintWriter(stringWriter));
		return stringWriter.toString();
	}
	
	/**
	 * <pre>
	 * 功能：打印调用的类链
	 * </pre>
	 */
	public static void printStackInvokeClass(PrintStream out) {
		try {
			Thread thr = Thread.currentThread();
			StackTraceElement[] ele = thr.getStackTrace();
			int index = 0;
			for (StackTraceElement ste : ele) {
				if (index++ > 2) {
					out.println(ste);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
